﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using Warehouse.API.Applcation.Command.WarehouseCommand;
using Warehouse.Domain;
using Warehouse.Domain.DTO;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;

namespace Warehouse.API.Controllers
{
    /// <summary>
    /// 仓库控制器(增删改)
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    
    public class WarehouseController : ControllerBase
    {

        private readonly IMediator mediator;

        public WarehouseController(IMediator mediator)
        {
            this.mediator = mediator;
        }


        /// <summary>
        /// 添加仓库信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> WarehouseAdd(CreateWarehouseCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();

            try
            {
                var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.office_id = sid;
                command.user_id = int.Parse(uid);
                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -100;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult<int>> BatchDelWarehous(BatchDelWarehouseCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();

            try
            {
                var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.office_id = sid;
                command.user_id = int.Parse(uid);
                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }




        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult<int>> UpdateWarehouse(UpdateWarehouseCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();

            try
            {
                var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.office_id = sid;
                command.user_id = int.Parse(uid);
                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }




         /// <summary>
         /// 批量修改
         /// </summary>
         /// <param name="command"></param>
         /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult<int>> BatchWarehouse(BatchWarehouseCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();

            try
            {
                var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.office_id = sid;
                command.user_id = int.Parse(uid);
                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
    }
}
